package cn.w.outdoor.lib.model;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import cn.w.outdoor.lib.common.CommonMethods;
import cn.w.outdoor.lib.common.CommonTimeMethods;

public class PCMSG {

    private final static int MSG_INTERCOM_MESSAGE = 0x10000008;
    private final static int MSG_INTERCOM_MESSAGE_LEN = 250;

    private byte[] pcmsg;

    public PCMSG() {
        pcmsg = new byte[324];
        CommonMethods.intInsertIntoBytes(pcmsg, 64, MSG_INTERCOM_MESSAGE);
        CommonMethods.intInsertIntoBytes(pcmsg, 68, MSG_INTERCOM_MESSAGE_LEN);
    }

    public PCMSG(String message) {
        this();
        setMessage(message);
    }

    public PCMSG(String message, Date date) {
        this();
        setMessage(message, date);
    }

    public void setMessage(String message) {
        setMessage(message, new Date());
    }

    public void setMessage(String message, Date date) {
        if (!"".equals(message) && message != null) {
//            byte[] contentMessage = UnicodeConvertUtil.convertToBytes(message);
            try {
                byte[] contentMessage = message.getBytes("GB2312");
//                System.out.println("消息内容：" + CommonMethods.byte2hex(contentMessage));
                System.arraycopy(contentMessage, 0, pcmsg, 72, Math.min(contentMessage.length, 200));
                byte[] timeBytes = CommonTimeMethods.getInstance().getFormattedTime("yyyy-MM-dd HH:mm:ss", date).getBytes();
                int timeBytesLength = timeBytes.length;
                System.arraycopy(timeBytes, 0, pcmsg, 272, Math.min(timeBytesLength, 50));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    public byte[] generateToBytes() {
        return pcmsg;
    }

}
